package com.lanou.dllo.soccernews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.greendao.Users;
import com.lanou.dllo.soccernews.database.UsersDBhelper;
import com.lanou.dllo.soccernews.utils.RegularUtil;

import java.util.List;

public class AddRegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText username, pwd, okpwd;
    private Button intohome;
    private String usernameString, pwdString, okpwdSting;
    private String tel;
    private boolean isUsername = false;//判断是否已经存在了
    private UsersDBhelper helper;
    private boolean isPwd = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_register);
        init();

        Intent intent = getIntent();
        tel = intent.getStringExtra("tel");

    }

    private void init() {
        username = (EditText) findViewById(R.id.username);
        pwd = (EditText) findViewById(R.id.pwd);
        okpwd = (EditText) findViewById(R.id.okpwd);
        intohome = (Button) findViewById(R.id.intohome);
        intohome.setOnClickListener(this);
        helper = UsersDBhelper.getInstance();
    }

    //String username, String password, String tel, Boolean sex, String address, String collect
    @Override
    public void onClick(View v) {
        usernameString = username.getText().toString();
        pwdString = pwd.getText().toString();
        okpwdSting = okpwd.getText().toString();
        isUsername = helper.getName(usernameString);//判断是否唯一
        isPwd = RegularUtil.checkPassword(this,pwdString);//判断密码是否符合规范

        switch (v.getId()) {
            case R.id.intohome:
                if (usernameString != null && usernameString.length()>0&&isUsername == true){
                    if (isPwd == true){
                        if ( pwdString.equals(okpwdSting)){
                            List<Users> users  = helper.getUsesToTel(tel);
                            Users user =  users.get(0);
                            user.setUsername(usernameString);
                            user.setPassword(pwdString);
                            helper.updateInfo(user);
                           Log.i("lanou00", helper.getUsersInfoList().toString() + "您的账号已经被注册过咯!");

                            Intent intent = new Intent(this,MyHomeActivity.class);
                            startActivity(intent);


                        }
                    }
                    else{
                        Toast.makeText(this,"请再输繁杂点吧,免的被盗啊",Toast.LENGTH_LONG).show();
                    }
                }
                else{
                    Toast.makeText(this,"用户名已经被注册过了",Toast.LENGTH_LONG).show();
                }

                    break;
        }
    }
}
